﻿using System;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
using JUtil;
using JUtil.Web.Extensions;
using TPPDDB.FestivalTicketSystem;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        QueryMaster();
    }

    #region grvMaster_RowDeleted
    protected void grvMaster_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        try
        {
            if (e.Exception != null)
            {
                Log.ReportError(e.Exception);

                e.ExceptionHandled = true;
            }
            else
            {
                QueryMaster();

                HideDetail();
            }
        }
        catch (Exception ex)
        {
            Log.ReportError(ex);
        }
    }
    #endregion

    #region grvMaster_PageIndexChanged
    protected void grvMaster_PageIndexChanged(object sender, EventArgs e)
    {
        try
        {
            QueryMaster();

            HideDetail();
        }
        catch (Exception ex)
        {
            Log.ReportError(ex);
        }
    }
    #endregion

    #region grvMaster_SelectedIndexChanged
    protected void grvMaster_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GridView grv = grvMaster;
            OrderedDictionary od = grv.ExtractSelectedValues();
            bool bCanEdit = CanEdit(od);
            QueryDetail(bCanEdit);
        }
        catch (Exception ex)
        {
            Log.ReportError(ex);
        }
    }
    #endregion

    #region grvMaster_RowDataBound
    protected void grvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;

        if (row.RowType != DataControlRowType.DataRow) return;

        OrderedDictionary od = row.ExtractValues();

        //Button BtnEdit = (Button)row.GetButton(GridviewCommand.Select);
        //bool bCanEdit = CanEdit(od);
        //BtnEdit.ToolTip = bCanEdit ? "Edit" : "View";
        //Button BtnDelete = (Button)row.GetButton(GridviewCommand.Delete);
        //BtnDelete.Visible = CanDelete(od);
        //Button BtnBallot = (Button)row.FindControl("BtnBallot");
        //BtnBallot.Visible = CanBallot(od);
    }
    #endregion

    #region dsMaster_ObjectCreating
    protected void dsMaster_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        e.ObjectInstance = MasterBLL;
    }
    #endregion

    #region QueryMaster
    protected void QueryMaster(int PageIndex)
    {
        grvMaster.PageIndex = PageIndex;
        QueryMaster();
    }

    protected void QueryMaster()
    {
        // setup QueryCondition
        SetMasterSelectParameters();

        // reset index
        grvMaster.SelectedIndex = -1;

        grvMaster.DataBind();
    }
    #endregion

    #region SetMasterSelectParameters
    protected void SetMasterSelectParameters()
    {
        MasterBLL.SelectParameters.Clear();

        MasterBLL.SelectParameters["ACTIVITY_ID"] = null;
    }
    #endregion

    #region HideDetail
    protected void HideDetail()
    {
    }
    #endregion

    #region CanEdit
    private bool CanEdit(OrderedDictionary od)
    {
        return true;
    }
    #endregion

    #region QueryDetail
    protected void QueryDetail(bool bCanEdit)
    {
    }
    #endregion

    #region MasterBLL
    private BLL_FESTIVAL_ACTIVITY _MasterBLL;
    protected BLL_FESTIVAL_ACTIVITY MasterBLL
    {
        get
        {
            _MasterBLL = _MasterBLL ?? new BLL_FESTIVAL_ACTIVITY();
            return _MasterBLL;
        }
    }
    #endregion

}
